package cn.cs.controller;

import cn.cs.common.*;
import cn.cs.pojo.ProductType;
import cn.cs.service.ProductTypeService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/productType")
public class ProductTypeController {
    @Resource
    private ProductTypeService service;


    /****
     * 查询商品分类信息
     * @param request
     * @param
     * @return
     */
    @RequestMapping("/getProType")
    @ResponseBody
    public Dto getProType(HttpServletRequest request, HttpServletResponse response){
        Dto dto=null;
        try {
            String token = request.getHeader("token");
            dto=service.getProType(token);
        }catch (Exception e){
            e.printStackTrace();
            return DtoUtil.returnFail("查询失败","0002");
        }
        return dto;
    }


    /****
     * 根据分类父id获取子分类
     * @param request
     * @param
     * @return
     */
    @RequestMapping("/getProTypeByPid")
    @ResponseBody
    public Dto getProTypeByPid( HttpServletRequest request, HttpServletResponse response){
        Dto dto=null;
        try {
            dto=service.getProTypeByPid(request);
        }catch (Exception e){
            e.printStackTrace();
            return DtoUtil.returnFail("查询失败","0002");
        }
        return dto;
    }

    /****
     * 根据分类查询详细商品信息
     * @param request
     * @param
     * @return
     */
    @RequestMapping("/getProByType")
    @ResponseBody
    public Dto getProByType( HttpServletRequest request, HttpServletResponse response){
        Dto dto=null;
        try {
            dto=service.getProByType(request);
        }catch (Exception e){
            e.printStackTrace();
            return DtoUtil.returnFail("查询失败","0002");
        }
        return dto;
    }
    /**
     * 分页查询 配件分类
     * @param pageNumber
     * @param pageSize
     * @return
     * @throws Exception
     * TODO 添加搜索
     */
    @RequestMapping(value = "/csPlatform/getProductTypeList")
    public Dto getProductTypeList(Integer pageNumber, Integer pageSize, String id)throws Exception{
        Map<String, Object> param = new HashMap<String,Object>();
        param.put("parentId", id);
        return DtoUtil.returnSuccess("获取成功", service.queryProductTypePageByMap(param, pageNumber, pageSize));
    }

    /**
     * 新增/编辑  配件分类
     * @param productType
     * @return
     */
    @RequestMapping(value = "/csPlatform/updateProductType")
    public Dto updateProductType(@RequestBody ProductType productType){
        try {
            String msg = "新增成功";
            String typeCode = "type_" + PingYinUtil.getFirstSpell(productType.getTypeName());
            productType.setTypeCode(typeCode);
            if (EmptyUtils.isEmpty(productType.getId())) {
                productType.setId(IdWorker.getId());
                productType.setTypeLevel("3");
                productType.setCreateTime(DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
                productType.setUpdateTime(productType.getCreateTime());
                service.addProductType(productType);
            } else {
                productType.setUpdateTime(DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
                service.modifyProductType(productType);
                msg = "修改成功";
            }
            return DtoUtil.returnSuccess(msg);
        }catch (Exception e){
            System.out.println(e.getCause().getMessage());
            e.printStackTrace();
            return DtoUtil.returnFail("操作失败，请重试", "001");
        }
    }


    /**
     * 获取配件分类 资源树
     * @return
     */
    @RequestMapping(value = "/csPlatform/getProductTypeTree")
    public Dto getProductTypeTree() {
        return DtoUtil.returnSuccess("操作成功",service.getTypeTree());
    }

    /**
     * 删除故障配件分类
     * @param ids
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/csPlatform/delProductType")
    public Dto delProductType(String ids)throws Exception{
        int i = 0;
        for(String id:ids.split(",")){
            i += service.deleteProductTypeById(id);
        }
        if(i>0){
            return DtoUtil.returnSuccess("操作成功", i);
        }else {
            return DtoUtil.returnFail("删除失败，有子分类或有商品，无法删除", "001");
        }
    }

}
